package lesson3.slides.io;

import java.io.*;

public class SerExample implements Serializable {

    private DataObject[] objects = {
        new DataObject(981), 
        new DataObject(3), 
        new DataObject(-98)
    };
    
    private String id = "Container";

    public String toString() {
        String s = "";
        for (int i = 0; i < 3; i++) {
            s += objects[i];
        }
        return id + " " + s;
    }

    public static void main(String[] args) {
        SerExample se = new SerExample();
        try {
            System.out.println("Created object");
            System.out.println(se);
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("somewhere"));
            out.writeObject(se);
            out.close();
            se = null; // object is now out of scope.
            System.out.println("Written and destroyed.");
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("somewhere"));
            SerExample newSE = (SerExample) in.readObject();
            in.close();
            System.out.println("Read and recovered object.");
            System.out.println(newSE);
        } catch (Exception e) {
            System.err.println(e);
        }
    } //end main of SerExample
} //end class SerExample
